package ccm.tools;

import ccm.common.CCMConfigManager;
import ccm.common.Utils;

import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Arrays;

public class ImageViewer2 extends ImageViewer
{
    protected int[] fileListIndexes;
    protected int[] fileListIndexesMin;
    protected int[] fileListIndexesMax;

    public static void main()
    {
        main(new String[0]);
    }

    public static void main(String[] args)
    {
        System.out.println(Arrays.toString(args));
        new ImageViewer2();
    }

    protected static int[] split(File file)
    {
        String[] parts=file.getName().split("[-.]");
        final int dim=parts.length-1;
        final int[] ans=new int[dim];
        for(int i=0;i<dim;i++)
        {
            ans[i]=Integer.parseInt(parts[i]);
        }
        return ans;
    }

    protected static boolean is_same(int[] a,int[] b)
    {
        assert a.length==b.length;
        for(int i=0;i<a.length;i++)
            if(a[i]!=b[i])
                return false;
        return true;
    }

    protected void updateMinMax(int[] a)
    {
        assert a.length==fileListIndexesMin.length;
        assert a.length==fileListIndexesMax.length;
        for(int i=0;i<a.length;i++)
        {
            fileListIndexesMin[i]=Math.min(fileListIndexesMin[i],a[i]);
            fileListIndexesMax[i]=Math.max(fileListIndexesMax[i],a[i]);
        }
    }

    protected void updateIndexI()
    {
        for(int i=0;i<fileListIndexes.length;i++)
            fileListIndexes[i]=Math.min(Math.max(fileListIndexes[i],fileListIndexesMin[i]),fileListIndexesMax[i]);

        for(int i=0;i<fileList.length;i++)
            if(is_same(split(fileList[i]),fileListIndexes))
                fileListI=i;

    }


    synchronized protected void updateFileDir()
    {
        try
        {
            fileList=Utils.getFileList(CCMConfigManager.getString("ccm/tools/"+this.getClass().getName()+"/lastDir"),".png");
            System.out.println(Arrays.toString(fileList));
            if(fileList.length==0)
                throw new NoSuchFieldException();
            fileListIndexes=split(fileList[0]);
            fileListIndexesMin=fileListIndexes.clone();
            fileListIndexesMax=fileListIndexes.clone();
            for(int i=1;i<fileList.length;i++)
                updateMinMax(split(fileList[i]));
            updateIndexI();
            updateImage();
        }catch(NoSuchFieldException e)
        {
            selectFileDir();
        }
    }

    synchronized public void keyPressed(KeyEvent e)
    {
        if(fileList==null)
        {
            System.out.println("null fileList");
            return;
        }
        if(e.getKeyCode()==KeyEvent.VK_A)
        {
            fileListIndexes[1]-=1;
        }
        else if(e.getKeyCode()==KeyEvent.VK_D)
        {
            fileListIndexes[1]+=1;
        }
        else if(e.getKeyCode()==KeyEvent.VK_W)
        {
            fileListIndexes[0]-=1;
        }
        else if(e.getKeyCode()==KeyEvent.VK_S)
        {
            fileListIndexes[0]+=1;
        }
        else if(e.getKeyCode()==KeyEvent.VK_F)
        {
            updateFileDir();
            updateImage();
        }
        updateIndexI();
        updateImage();
    }
}
